//
//  mainCollectionViewCell.m
//  UICollectionView
//
//  Created by wonders on 2024/12/2.
//

#import "mainCollectionViewCell.h"

@implementation mainCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];//调用父类的初始化方法
    if (self) {
        self.backgroundColor = [UIColor clearColor];//设置单元格的背景颜色为透明
        [self setupViews];//调用 setupViews 方法来初始化和添加子视图（图标和标题）
    }
    return self;
}//UICollectionViewCell 的初始化方法，用于创建新的单元格实例

- (void)setupViews {
    self.icon = [[UIImageView alloc] init];
    self.icon.contentMode = UIViewContentModeScaleAspectFit;//保持图片比例缩放
    [self.contentView addSubview:self.icon];//添加到contentView，contentView是UICollectionViewCell的主要内容区域
    
    self.title = [[UILabel alloc] init];
    self.title.font = [UIFont systemFontOfSize:12];//设置字体大小为12
    self.title.textColor = [UIColor blackColor];//设置颜色
    self.title.textAlignment = NSTextAlignmentCenter;//居中对齐
    [self.contentView addSubview:self.title];//添加到视图上
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat iconX = (self.contentView.bounds.size.width - 44) / 2;
    CGFloat iconY = (self.contentView.bounds.size.height - 44) / 2;
    self.icon.frame = CGRectMake(iconX, iconY, 44, 44);//计算icon的x，y坐标以确保在contentView居中
    
    CGFloat titleX = 0;
    CGFloat titleY = iconY + 44 + 5; //title距离icon底部5点的位置
    self.title.frame = CGRectMake(titleX, titleY, self.contentView.bounds.size.width, 13);//设置字体居中
}

#pragma mark -layUI 懒加载icon和title属性，并初始化
- (UIImageView *)icon {
    if (!_icon) {
        _icon = [[UIImageView alloc] init];
    }
    return _icon;
}

- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
    }
    return _title;
}

@end
